Skip to content

mm/mempool: fixes and mempool_zallocate() - #22206

Merged
GUIDINGLI merged 8 commits into
apache:masterfrom
huojianchaoyy:nx-mm-heap-mempool-fixes-v1
Nov 25, 2026
Merged

GUIDINGLI merged 8 commits into
apache:masterfrom
huojianchaoyy:nx-mm-heap-mempool-fixes-v1

Conversation

@huojianchaoyy

@huojianchaoyy huojianchaoyy commented Nov 23, 2026 •

Copy link
Copy Markdown

Note: Please adhere to Contributing Guidelines.

Summary

Memory pool fixes and a small addition from Xiaomi Vela.

The first version of this PR also had the other mm changes; as asked in the review, they are now in separate PRs: #22275 (mm_heap: memdump, mm_foreach), #22276 (mm/map), #22277 (mm/iob), #22278 (KASAN), #22279 (UBSAN), #22280 (sim heap), #22281 (CMake section names) and #22282 (Tasking). "mm/iob: add null check for iob2 in iob_clone_partial" and "mm/iob: handle overlapping copyin buffers" went into master with #22204.

  • mempool:fix pool->nalloc missing lock: mempool_info_task() reads pool->nalloc under the pool lock.
  • mm/mempool: MISRA-C 2012 9.1, variables must be initialized before use: semcount is initialized before nxsem_get_value() fills it in mempool_release() and mempool_info().
  • mm/mempool: Avoid using global variables.: the lock that only mempool_init() uses becomes a static of that function (MISRA-C 2012 8.9).
  • mempool: improve debuggability: mempool_remove_queue() checks the next block before it changes the queue head, so the original data is still there when a check fails.
  • coverity/mempool: fix mempool_multiple_init type covert from int into size_t: the size of the pool array is kept in a size_t.
  • mm/mempool: add mempool_zallocate interface: allocate a zeroed block. mm/mempool: add the missing blank line in mempool_zallocate() follows it (nxstyle).
  • include/mempool: remove unnecessary header file to avoid redefine: <nuttx/mm/mempool.h> does not include <nuttx/list.h> any more.

Impact

  • New API: mempool_zallocate().
  • <nuttx/mm/mempool.h> no longer includes <nuttx/list.h>; code that got the list API through it has to include <nuttx/list.h> itself.

Testing

Host: Ubuntu 22.04 x86_64, GCC 13.4.0 (sim, aarch64-none-elf), Arm GNU Toolchain 13.2.rel1, CMake 3.23 + Ninja 1.10. Base: master 25b649e.

./tools/checkpatch.sh -c -u -m -g HEAD~8..HEAD passes.

sim:ostest (KASAN and UBSAN) with CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=32, so that small allocations come from the heap's memory pools: a 320 s run completes one ostest pass with no KASAN/UBSAN report and the same output as master. The same with the default configuration.

sim:nsh with CONFIG_MM_BACKTRACE=8 CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=32 CONFIG_MM_HEAP_BIGGEST_COUNT=4: free, echo mempool > /proc/memdump and echo used > /proc/memdump work.

Builds: CMake sim:nsh (options above), sim:ostest, qemu-armv8a:nsh_smp, mps2-an500:nsh; Make sim:nsh with the same options.

Not tested: real hardware.

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please split the PR in logical separate PR for separate areas:
memdump
mempool
map
iob
kasan
ubsan

@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

mps2-an500-citest

  • flash: .text -8 B (-0.0%, 247,312 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +8 B (+0.0%, 116,472 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.mempool_info +4 B (+0.0%, 377,636 B)

qemu-intel64

  • Code: .text +6 B (+0.0%, 8,707,095 B)

s698pm-dkit

  • Code: .text +128 B (+0.0%, 409,024 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

GUIDINGLI
GUIDINGLI previously approved these changes Nov 24, 2026
@huojianchaoyy
huojianchaoyy force-pushed the nx-mm-heap-mempool-fixes-v1 branch from 694d32d to 52291b0 Compare November 24, 2026 06:54
@huojianchaoyy

Copy link
Copy Markdown
Author

@jerpelea thanks for the review. I'd prefer to keep this as one PR:

  • Apart from three build commits (see below), every commit changes mm/ or its headers, the sim heap, or drivers/video/fb.c, which is the only in-tree caller of vm_map_region()/vm_unmap_region(). Each commit is one small change, about 16 changed lines on average, and the description lists every commit under its area, so the series can be reviewed one commit at a time.
  • Split by those areas, most PRs would be tiny: iob 1 commit, ubsan 2, memdump 3, kasan 4, map 6, mempool 8, between 5 and 143 changed lines each. Another 8 commits (the sim heap, mm_foreach(), kmalloc.h and the three build commits) belong to none of the six. Each of the mm PRs would go through the full CI again (58 checks on this one).
  • Two of the iob commits, "mm/iob: add null check for iob2 in iob_clone_partial" and "mm/iob: handle overlapping copyin buffers", are now in master through iob: Eliminate the iob_qentry_s pool and use pointer low-bit flags #22204, so I dropped them in the latest push.

If you would rather not have the build changes here, I can move "nuttx: Adapt for Tasking compiler", "cmake: fix section not take effect" and the fs_heap style fix that goes with them to a separate PR, and retitle this one "mm: fixes and small additions" so it doesn't read as six topics. If that works for you, could you update your review?

@jerpelea

Copy link
Copy Markdown
Contributor

@jerpelea thanks for the review. I'd prefer to keep this as one PR:

* Apart from three build commits (see below), every commit changes mm/ or its headers, the sim heap, or drivers/video/fb.c, which is the only in-tree caller of vm_map_region()/vm_unmap_region(). Each commit is one small change, about 16 changed lines on average, and the description lists every commit under its area, so the series can be reviewed one commit at a time.

* Split by those areas, most PRs would be tiny: iob 1 commit, ubsan 2, memdump 3, kasan 4, map 6, mempool 8, between 5 and 143 changed lines each. Another 8 commits (the sim heap, mm_foreach(), kmalloc.h and the three build commits) belong to none of the six. Each of the mm PRs would go through the full CI again (58 checks on this one).

* Two of the iob commits, "mm/iob: add null check for iob2 in iob_clone_partial" and "mm/iob: handle overlapping copyin buffers", are now in master through [iob: Eliminate the iob_qentry_s pool and use pointer low-bit flags #22204](https://github.com/apache/nuttx/pull/22204), so I dropped them in the latest push.

If you would rather not have the build changes here, I can move "nuttx: Adapt for Tasking compiler", "cmake: fix section not take effect" and the fs_heap style fix that goes with them to a separate PR, and retitle this one "mm: fixes and small additions" so it doesn't read as six topics. If that works for you, could you update your review?

please split the PR
-release notes are important and a PR that touches multiple areas
-a huge PR is hard to document, hard to understand and maintain downstream

anjiahao1 and others added 8 commits November 24, 2026 20:05
pool->nalloc need lock to protect

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Initialize semcount before nxsem_get_value() fills it in
mempool_release() and mempool_info(), so that it has a value even if
the call fails.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
According to MISRA-C2012 8.9, An object should be defined at block scope if its identifier only appears in a single function

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Check before modify, so when check fails, we can still get the original data.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
… size_t

mempool_multiple_init() kept the size of the pool array, a size_t, in
the int ret.  Use a size_t variable for it.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: makejian <makejian@xiaomi.com>
mempool_zallocate() allocates a block like mempool_allocate() and
clears its pool->blocksize bytes, for callers that need a zeroed
block.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
nxstyle wants a blank line after the declaration of blk.  No
functional change.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: huojianchao <huojianchao@xiaomi.com>
compile error:
In file included from include/nuttx/mm/mempool.h:32,
                 from include/nuttx/mm/iob.h:36,
                 from include/nuttx/net/net.h:43,
                 from include/nuttx/net/netdev_lowerhalf.h:39,
                 from platform/realtek_ieee80211/realtek_netdev.c:5:
static inline void list_add_tail(struct list_head *newitem, struct list_head *head)

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
@huojianchaoyy

Copy link
Copy Markdown
Author

@jerpelea Split as requested. This PR now has only the mempool part; the other parts are in their own PRs:

Could you take another look?

@github-actions github-actions Bot added Area: Memory Management Memory Management issues Size: S The size of the change in this PR is small and removed Arch: simulator Issues related to the SIMulator Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Nov 24, 2026
@GUIDINGLI
GUIDINGLI dismissed jerpelea’s stale review November 25, 2026 04:15

Already splited

@GUIDINGLI
GUIDINGLI merged commit 46bfd41 into apache:master Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Memory Management Memory Management issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants